package top.luckyluxcis.api;

import lombok.Data;
import lombok.experimental.UtilityClass;
import top.luckyluxcis.RollApi;
import top.luckyluxcis.consts.ImageType;
import top.luckyluxcis.type.Dictionary;
import top.luckyluxcis.type.IdCard;
import top.luckyluxcis.type.ShortLink;
import top.luckyluxcis.type.VerifyCode;
import top.luckyluxcis.util.Base64Util;
import top.luckyluxcis.util.MapUtil;

import java.util.Map;

/**
 * @author Zhuang
 * @since 2023/9/25
 */
@SuppressWarnings("unused")
@UtilityClass
public class SystemUtil {
    private static final RollApi API = RollApi.getInstance();

    /**
     * 获取不重复长ID信息
     *
     * @return {@link String} 不重复16位字符id
     */
    public static String noRepeatLongId() {
        return ((ID) API.get("/tools/no_repeat_id/long", MapUtil.empty(), ID.class)).getId();
    }

    /**
     * 获取不重复短ID信息
     *
     * @return {@link String} 不重复8位字符id
     */
    public static String noRepeatShortId() {
        return ((ID) API.get("/tools/no_repeat_id/short", MapUtil.empty(), ID.class)).getId();
    }

    /**
     * 获取服务器时间
     *
     * @return {@link Long} 服务器当前时间戳
     */
    public static Long time() {
        return ((ID) API.get("/tools/no_repeat_id/short", MapUtil.empty(), ID.class)).getTime();
    }

    /**
     * 生成随机长度的图片验证码。
     *
     * @return {@link VerifyCode}
     */
    public static VerifyCode verifyCode(int len, ImageType type) {
        Map<String, Object> params = MapUtil.<String, Object>builder()
                .put("len", len)
                .put("type", type.getCode())
                .build();
        return API.get("/verifycode/code", params, Dictionary.ConvertInfo.class);
    }

    /**
     * 长url生成短链接
     *
     * @param url 待生成短链的url地址
     * @return {@link ShortLink}
     */
    public static ShortLink createShortLink(String url) {
        Map<String, Object> params = MapUtil.<String, Object>builder()
                .put("url", Base64Util.encode(url))
                .build();
        return API.get("/verifycode/code", params, ShortLink.class);
    }

    /**
     * 获取身份证归属地信息，本服务并不能识别出身份证的真伪，仅依编码及格式进行识别判断
     *
     * @param idcard 身份证
     * @return {@link IdCard}
     */
    public static IdCard idCardSearch(String idcard) {
        Map<String, Object> params = MapUtil.<String, Object>builder()
                .put("idcard", idcard)
                .build();
        return API.get("/idcard/search", params, IdCard.class);
    }

    @Data
    public static class ID {
        private String id;
        private Long time;
    }
}
